//   Copyright 2012,2013,2014 Vaughn Vernon, zhenguang zhu
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.sumer.psychology.common;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * 守卫工具
 */
public final class Assert {

    private Assert() {
        super();
    }

    public static void equals(Object anObject1, Object anObject2) {
        equals(anObject1, anObject2, "参数不相等");
    }

    public static void equals(Object anObject1, Object anObject2, String aMessage) {
        if (!anObject1.equals(anObject2)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void isFalse(boolean aBoolean) {
        isFalse(aBoolean, "参数应该为否");
    }

    public static void isFalse(boolean aBoolean, String aMessage) {
        if (aBoolean) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void length(String aString, int aMaximum) {
        length(aString, aMaximum, "参数长度错误");
    }

    public static void length(String aString, int aMaximum, String aMessage) {
        if (StringUtils.isBlank(aString)) {// 如果为空就可以不校验
            return;
        }

        int length = aString.trim().length();
        if (length > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void length(String aString, int aMinimum, int aMaximum) {
        length(aString, aMinimum, aMaximum, "参数长度错误");
    }

    public static void length(String aString, int aMinimum, int aMaximum, String aMessage) {
        int length = aString.trim().length();
        if (length < aMinimum || length > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void notEmpty(String aString) {
        notEmpty(aString, "参数为空");
    }

    public static void notEmpty(String aString, String aMessage) {
        if (StringUtils.isBlank(aString)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void anyEmpty(String... strings) {
        if (ArrayUtils.isNotEmpty(strings)) {
            for (String s : strings) {
                notEmpty(s, "参数部分为空");
            }
        }
    }

    public static void notEmpty(Collection aCollection) {
        notEmpty(aCollection, "集合为空");
    }

    public static void notEmpty(Collection aCollection, String aMessage) {
        if (CollectionUtils.isEmpty(aCollection)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void notEquals(Object anObject1, Object anObject2) {
        notEquals(anObject1, anObject2, "参数相等");
    }

    public static void notEquals(Object anObject1, Object anObject2, String aMessage) {
        if (anObject1.equals(anObject2)) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void notNull(Object anObject) {
        notNull(anObject, "参数不能为null");
    }

    public static void notNull(Object anObject, String aMessage) {
        if (anObject == null) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void range(int aValue, int aMinimum, int aMaximum) {
        range(aValue, aMinimum, aMaximum, "区间错误");
    }

    public static void range(int aValue, int aMinimum, int aMaximum, String aMessage) {
        if (aValue < aMinimum || aValue > aMaximum) {
            throw new IllegalArgumentException(aMessage);
        }
    }

    public static void isTrue(boolean aBoolean) {
        isTrue(aBoolean, "参数应该为否");
    }

    public static void isTrue(boolean aBoolean, String aMessage) {
        if (!aBoolean) {
            throw new IllegalArgumentException(aMessage);
        }
    }

}
